﻿package com.tomvian.twodee.sideon.actions
{
	import flash.display.DisplayObject;
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.Actor;
	import com.tomvian.twodee.common.interactions.Interaction;
	import com.tomvian.twodee.common.interactions.Walls;
	import com.tomvian.twodee.sideon.SideOnActor;
	import com.tomvian.maths.Vector2D;
	
	/**
	 * Platforming
	 * @author Thomas Vian
	 */
	public class Platforming extends Action 
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public static const STILL:uint = 	0;
		public static const RUNNING:uint = 	1;
		public static const JUMPING:uint = 	2;
		public static const FALLING:uint = 	3;
		
		public static const DISPLAY_STILL:String = 		"still";
		public static const DISPLAY_RUNNING:String = 	"running";
		public static const DISPLAY_JUMPING:String = 	"jumping";
		public static const DISPLAY_FALLING:String = 	"falling";
		
		public var groundAccel:Number;
		public var groundFriction:Number;
		public var airAccel:Number;
		public var jumpSpeed:Number;
		
		public var ground:Interaction;
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Platforming(actor:Actor,
									ground:Interaction,
									groundAccel:Number, 
									groundFriction:Number, 
									airAccel:Number,
									jumpSpeed:Number)
		{
			super(actor);
			
			this.ground = ground;
			this.groundAccel = groundAccel;
			this.groundFriction = groundFriction;
			this.airAccel = airAccel;
			this.jumpSpeed = jumpSpeed;
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function start():void 
		{
			if (enabled)
			{
				if (ground.interacting)
				{
					if (actorMass.velocity.x > 0.2 || actorMass.velocity.x < -0.2) 	state = RUNNING;
					else															state = STILL;
				}
				else if (state != FALLING) 
				{
					state = FALLING;
				}
			}
		}
		
		override public function act():void 
		{
			if (enabled)
			{
				if (ground.interacting)
				{
					actorMass.friction.x *= groundFriction;
					
					if (actorController.right) 	actorMass.velocity.x += groundAccel;
					if (actorController.left) 	actorMass.velocity.x -= groundAccel;
					
					if (actorMass.velocity.x > 0.2 || actorMass.velocity.x < -0.2) 	state = RUNNING;
					else															state = STILL;
					
					if (actorController.jump) 
					{
						actorMass.velocity.y = jumpSpeed;
						state = JUMPING;
					}
				}
				else
				{
					if (actorController.right) 	actorMass.velocity.x += airAccel;
					if (actorController.left) 	actorMass.velocity.x -= airAccel;
					
					if(state != JUMPING && state != FALLING) state = FALLING;
				}
			}
		}
		
		override public function display():void 
		{
			switch(state)
			{
				case STILL: 	actorState.state = DISPLAY_STILL; 		break;
				case RUNNING: 	actorState.state = DISPLAY_RUNNING; 	break;
				case JUMPING: 	actorState.state = DISPLAY_JUMPING; 	break;
				case FALLING: 	actorState.state = DISPLAY_FALLING; 	break;
			}
		}
	}
}